package cn.lyvust.gui.computer;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Solution {
    public static void main(String[] args) {
        MyFrame frame = new MyFrame();
    }
}
class MyFrame extends JFrame {
    public MyFrame() {
        TextField textField1 = new TextField(10);
        TextField textField2 = new TextField(10);
        TextField textField3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");

        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        //监听响应函数
        button.addActionListener(new MyActionListener(textField1, textField2, textField3));
        setLayout(new FlowLayout());

        setVisible(true);
        setTitle("Java计算器");
        this.setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }
}

class MyActionListener implements ActionListener {
    private TextField textField1, textField2, textField3;

    public MyActionListener(TextField textField1, TextField textField2, TextField textField3) {
        this.textField1 = textField1;
        this.textField2 = textField2;
        this.textField3 = textField3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int a = Integer.parseInt(textField1.getText());
        int b = Integer.parseInt(textField2.getText());
        textField3.setText("" + (a + b));
//        textField1.setText("");
//        textField2.setText("");
    }
}